package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetMealServicelmpl implements SetMealService
{
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Override
    public void saveWithDish(SetmealDTO setmealDTO)
    {
        //往套餐表插入1条数据
            //把前端的数据复制过来
            Setmeal setmeal=new Setmeal();
            BeanUtils.copyProperties(setmealDTO,setmeal);
            //设置状态为：停售
            setmeal.setStatus(StatusConstant.DISABLE);
            setMealMapper.insert(setmeal);
            //获取插入成功后自动产生的id
            long setmealId=setmeal.getId();
        //往套餐菜品表插入N条数据
            //从DTO中拿到子信息
            List<SetmealDish> setmealdishes=setmealDTO.getSetmealDishes();
            //判断数量关系
            if(!setmealdishes.equals(null) && setmealdishes.size()>0)
            {
                for (SetmealDish setmealdish : setmealdishes)
                {
                    setmealdish.setSetmealId(setmealId);
                    setMealDishMapper.insert(setmealdish);
                }
            }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        //开始分页查询（页码，每页的记录数）
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //用page对象接收
        Page<Setmeal> page=setMealMapper.pageQuery(setmealPageQueryDTO);
        //拿到总数和记录集合
        long total= page.getTotal();
        List<Setmeal> records=page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 修改套餐的状态
     * @param status
     */
    @Override
    public void startOrstop(Integer status,long id)
    {
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setMealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids)
    {
        for (Long id: ids)
        {
            Setmeal setmeal=setMealMapper.getById(id);
            if(setmeal.getStatus()==StatusConstant.ENABLE)
            {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐表里的数据
        setMealMapper.deleteBatch(ids);
        //删除套餐-菜品关系表里的数据
        setMealDishMapper.deleteBatch(ids);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void updateWithDish(SetmealDTO setmealDTO)
    {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐的基本信息
        setMealMapper.update(setmeal);

        //修改套餐的“套餐-菜品”关系（先删除后新增）
            //删除旧的
            setMealDishMapper.deleteById(setmeal.getId());
            //增加新的
            List<SetmealDish> dishes=setmealDTO.getSetmealDishes();
            if( !dishes.equals(null) && dishes.size()>0)
            {
                dishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
                });
                setMealDishMapper.insertBatch(dishes);
            }

    }

    /**
     * 根据id查询套餐的基本信息和包含的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdwithDish(long id)
    {
        Setmeal setmeal=setMealMapper.getById(id);
        List<SetmealDish> setmealDishes=setMealDishMapper.getByIdwithDish(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
